import { resetPassword } from '@/api'
import { Button } from '@/components/ui/button'
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
} from '@/components/ui/dialog'
import {
  Form,
  FormControl,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { zodResolver } from '@hookform/resolvers/zod'
import { message } from 'antd'
import { useEffect } from 'react'
import { useForm } from 'react-hook-form'
import { z } from 'zod'

const formSchema = z.object({
  userIdcardNum: z.string().refine(
    (val) => {
      const idCardRegex =
        /^[1-9]\d{5}(19\d{2}|20[0-2]\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/
      return idCardRegex.test(val)
    },
    { message: '请输入有效的18位身份证号' },
  ),
  pinPwd: z.string({ message: '请输入新密码' }),
})

interface ResetPasswordDialogProps {
  open: boolean
  onOpenChange: (open: boolean) => void
}

export function ResetPasswordDialog(props: ResetPasswordDialogProps) {
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      userIdcardNum: '',
      pinPwd: '',
    },
  })

  const handleSubmit = async (values: z.infer<typeof formSchema>) => {
    const res = await resetPassword(values)
    if (res.data.errcode !== 0) {
      message.error(res.data.errmsg)
    } else {
      message.success('修改成功')
      props.onOpenChange(false)
    }
  }

  useEffect(() => {
    if (!props.open) form.reset()
  }, [form, props.open])

  return (
    <Dialog open={props.open} onOpenChange={props.onOpenChange}>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>重置密码</DialogTitle>
          <DialogDescription>
            如果您忘记了在信息采集时上传的签署密码，可以在此重置密码
          </DialogDescription>
        </DialogHeader>
        <Form {...form}>
          <form
            className='space-y-3'
            onSubmit={form.handleSubmit(handleSubmit)}
          >
            <FormField
              control={form.control}
              name='userIdcardNum'
              render={({ field }) => {
                return (
                  <FormItem>
                    <FormLabel>身份证号</FormLabel>
                    <FormControl>
                      <Input {...field} placeholder='18位身份证号' />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )
              }}
            />
            <FormField
              control={form.control}
              name='pinPwd'
              render={({ field }) => {
                return (
                  <FormItem>
                    <FormLabel>新密码</FormLabel>
                    <FormControl>
                      <Input {...field} placeholder='新的密码' />
                    </FormControl>
                    <FormMessage />
                  </FormItem>
                )
              }}
            />
            <DialogFooter>
              <DialogClose asChild>
                <Button variant='outline' className='px-8'>
                  取消
                </Button>
              </DialogClose>
              <Button type='submit' className='px-8'>
                确认
              </Button>
            </DialogFooter>
          </form>
        </Form>
      </DialogContent>
    </Dialog>
  )
}
